package com.infraware.supermanbooks.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

import android.os.AsyncTask;

public class FileExplorer {

    private OnFileFoundListener listener;

    public FileExplorer(OnFileFoundListener listener) {
        this.listener = listener;
    }

    public void getFiles(String dirPath, String extension) {
        FileSearch task = new FileSearch(dirPath, extension);
        task.execute(dirPath, extension);
    }

    private class FileSearch extends AsyncTask<String, String, Integer> {
        private int foundCount;
        private String dirPath;
        private String extension;
        private FileFilter filter;

        //        private FilenameFilter filter;

        public FileSearch(String dirPath, String extension) {
            this.dirPath = dirPath;
            this.extension = extension;

            filter = new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    if (pathname.isDirectory()) {
                        return true;
                    } else if (pathname.getAbsolutePath().endsWith(FileSearch.this.extension)) {
                        return true;
                    }
                    return false;
                }
            };
        }

        @Override
        protected Integer doInBackground(String... params) {
            File file = new File(dirPath);
            if (!file.exists())
                return 0;

            searchFolder(file);
            return foundCount;
        }

        private boolean searchFolder(File parent) {
            if (!parent.isDirectory())
                return false;

            File[] files = parent.listFiles(filter);
            if (files == null)
                return false;

            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    searchFolder(file);
                } else {
                    foundCount++;
                    if (listener != null) {
                        boolean continued = listener.onFound(file.getAbsolutePath());
                        if (!continued) {
                            return false;
                        }

                    }
                }
            }
            return false;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            if (listener != null && values != null) {
                listener.onFound(values[0]);
            }
        }

        @Override
        protected void onPostExecute(Integer result) {
            if (listener != null) {
                listener.onDone(result);
            }
        }
    }

    public interface OnFileFoundListener {

        public boolean onFound(String path);

        public void onDone(int foundCount);
    }
}
